<?php
require_once (APPLICATION_DIR."/library/external/XOAD/xoad.php");
class AdminalumninewsController extends DRX_Controller
{
	function xoadGetMeta()
	{
		XOAD_Client::mapMethods($this,array("deletenews"));
	}
	
	function deletenews($p_arrArgs)
	{
		$objNews = new AlumniNews();
		if ( $objNews -> deleteNews($p_arrArgs["news_id"]) )
			return "SUCCESS";
		else
			return "FAIL";
	}
	
	function editAction()
	{
		global $arrArgs;
		$objSmarty = DRX::load('smarty');
		$objNews = new AlumniNews();
		$this -> runXoadServer();
		$this -> pathWeb();
		
		if ( isset($_POST["title"]) )
		{
			$this -> doActionNews();
		}
		
		if ( !$arrInfoNews = $objNews -> getInfoNews($arrArgs[2]) )
		{
			redirect(APPLICATION_URL . "admin/alumni/news");
		}
		
		$objSmarty -> assign("arrInfoNews", $arrInfoNews);
		$objSmarty -> assign("fck_editor", $this -> createFCK("detail", $arrInfoNews["detail"]));
		$objSmarty -> assign("content", $objSmarty -> fetch("admin/alumni/news/frm_edit.tpl"));
		$objSmarty->display("admin/admin.tpl");
	}
	
	function addAction()
	{
		$objSmarty = DRX::load('smarty');
		$objNews = new AlumniNews();
		$this -> runXoadServer();
		$this -> pathWeb();
		
		if ( isset($_POST["title"]) )
		{
			$this -> doActionNews();
		}

		$objSmarty -> assign("fck_editor", $this -> createFCK("detail"));
		$objSmarty -> assign("content", $objSmarty -> fetch("admin/alumni/news/frm_add.tpl"));
		$objSmarty->display("admin/admin.tpl");
	}
	
	function doActionNews()
	{
		$objNews = new AlumniNews();
		$arrForm = $_POST;
		//upload hinh
		include "IO_Upload.php";
		$strPathUpload = APPLICATION_DIR . '/images/news';
		//echo $strPathUpload;
		$objUpload = new IO_Upload(true, $strPathUpload, 0);
		$objUpload -> setPrefixThumb(time() . "_");
		$objUpload -> uploadImage('image', $strNameImage);
		//echo $objUpload -> getMsgError();
		if ( $strNameImage != '' )
		{
			$objUpload -> setPrefixThumb("solvay_");
			$objUpload -> createThumbnail(SIZE_THUMBNAIL_NEWS, $strPathUpload, $strNameImage);	
			
			$objUpload -> setPrefixThumb("small_");
			$objUpload -> createThumbnail(SIZE_THUMBNAIL_SMALL, $strPathUpload, $strNameImage);	
			
			$arrForm['image'] = $strNameImage;
		}//end if
		else
			$arrForm['image'] = '';
			
		if ( $_POST["action"] == "add" )
		{
			$objNews -> addNews($arrForm);
		}
		else if ( $_POST["action"] == "update" )
		{
			$objNews -> updateNews($arrForm);
		}
		redirect(APPLICATION_URL . "admin/alumni/news");
	}
	
	function listAction()
	{
		global $arrArgs;
		$objSmarty = DRX::load('smarty');
		$objNews = new AlumniNews();
		$this -> runXoadServer();
		$this -> pathWeb();
		
		define("RESULT_PER_PAGE_NEWS", 20);
		$intPage = isset($arrArgs[2]) ? $arrArgs[2] : 1;
		$intStart = ($intPage-1)*RESULT_PER_PAGE_NEWS;
		$objListNews = $objNews -> getListNews($intStart, RESULT_PER_PAGE_NEWS);
		
		//phan trang
		include "IO_Page.php";
		$intTotal= $objListNews -> count('distinct');
		$objNaviPage = new IO_Page(5, RESULT_PER_PAGE_NEWS, 'onPage');
		$strNaviPage = $objNaviPage -> createPageNavigator($intPage, $intTotal, "");		
		$objSmarty -> assign("NAVIGATION_PAGE", $strNaviPage);
		$objSmarty -> assign("TOTAL", $intTotal);
		
		$objSmarty -> assign("objListNews", $objListNews);
		$objSmarty -> assign("intStart", $intStart);
		
		$objSmarty -> assign("content", $objSmarty -> fetch("admin/alumni/news/list_news.tpl"));
		$objSmarty->display("admin/admin.tpl");
	}
	
	function indexAction()
	{
		$this -> listAction();
	}
	
	function createFCK($p_strName, $p_strValue = "")
	{
		$oFCKeditor = new FCKeditor($p_strName) ;
		$oFCKeditor->BasePath = FCK_BASE_PATH;
		$oFCKeditor->Height = FCK_DEFAULT_HEIGHT;
		$oFCKeditor->Value = $p_strValue;
		$fck = $oFCKeditor->Create();
		return $fck;
	}
	
	function pathWeb()
	{
		$objSmarty = DRX::load('smarty');
		$list_path = array();
		$prefix_url             = APPLICATION_URL . ADMIN_URL;        
    $list_path[0]['url']    = $prefix_url;
    $list_path[0]['title']  = ADMIN_HOME_PATH_WEB_TITLE;
		$last_path['url']    = $prefix_url . "/alumni/news";
    $last_path['title']  = "Alumni News";
		
		$objSmarty -> assign("list_path",$list_path);
		$objSmarty -> assign("last_path",$last_path);
		$path_web = $objSmarty->fetch("admin/path_web.tpl");
	  $objSmarty -> assign("path_web",$path_web);
	}
	
	function runXoadServer()
	{
		$objSmarty = DRX::load('smarty');
		
		if ( XOAD_Server::runServer() )
		{
			exit();
		}
		
		$objSmarty -> assign("xoad_header", XOAD_Utilities::header(APPLICATION_URL . "library/external/XOAD"));	
		$objSmarty -> assign("xoad_handler", XOAD_Client::register($this, "/Adminalumninews"));
	}	
}
?>